package cn.com.connected.service.impl;

import cn.com.connected.base.exception.BusinessException;
import cn.com.connected.base.exception.ErrorCode;
import cn.com.connected.base.utils.IpTools;
import cn.com.connected.common.config.CurrentData;
import cn.com.connected.common.emnus.StatusType;
import org.springframework.stereotype.Service;
import cn.com.connected.service.DynamicService;
import cn.com.connected.base.params.page.PageParam;
import cn.com.connected.base.params.page.BaseSearchDto;
import cn.com.connected.base.params.page.PageResult;
import cn.com.connected.base.utils.BeanCopyUtils;
import cn.com.connected.dto.DynamicDto;
import cn.com.connected.dto.search.DynamicSearchDto;
import cn.com.connected.entity.DynamicEntity;
import cn.com.connected.mapper.DynamicMapper;
import cn.com.connected.vo.DynamicVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;

/**
 * 动态
 *
 * @author sms
 * @email sms@live.com
 * @date 2025-04-10 16:15:14
 */
@Slf4j
@Service("dynamicService")
public class DynamicServiceImpl extends ServiceImpl<DynamicMapper, DynamicEntity> implements DynamicService {
    @Resource
    private DynamicMapper dynamicMapper;

    @Override
    public DynamicVo dynamicAdd(DynamicDto dynamicDto) {
        DynamicEntity entity = new DynamicEntity();
        BeanCopyUtils.copy(dynamicDto,entity);
        entity.setMemberId(CurrentData.getMemberId());
        entity.setState(StatusType.t1.getValue());
        dynamicMapper.insert(entity);
        return dynamicInfo(entity.getId());
    }

    @Override
    public void dynamicEdit(String id, DynamicDto dynamicDto) {
        DynamicEntity entity = dynamicMapper.selectById(id);
        BeanCopyUtils.copy(dynamicDto,entity);
        entity.setId(id);
        dynamicMapper.updateById(entity);
    }

    @Override
    public DynamicVo dynamicInfo(String id) {
        DynamicEntity entity = dynamicMapper.selectById(id);
        return BeanCopyUtils.copy(entity,new DynamicVo());
    }

    @Override
    public void dynamicDel(String id) {
        DynamicEntity entity = dynamicMapper.selectById(id);
        if (entity ==null || entity.getMemberId()==null || !entity.getMemberId().equals(CurrentData.getMemberId())){
            throw new BusinessException(ErrorCode.WARING,"无权删除");
        }
        entity.setState(StatusType.t0.getValue());
        dynamicMapper.updateById(entity);
    }

    @Override
    public PageResult<DynamicVo> dynamicPage(BaseSearchDto<DynamicSearchDto> searchParams) {
        LambdaQueryWrapper<DynamicEntity> wrapper = Wrappers.lambdaQuery();

        wrapper.eq(DynamicEntity::getState,StatusType.t1.getValue());

        PageParam pageParam = searchParams.getPageParam();
        PageHelper.startPage(pageParam.getPageIndex(), pageParam.getPageSize());
        DynamicSearchDto searchDto = searchParams.getSearchOption();
        wrapper.eq(StringUtils.isNotEmpty(searchDto.getMemberId()),DynamicEntity::getMemberId,searchDto.getMemberId());

        wrapper.orderByDesc(Arrays.asList(DynamicEntity::getCreateTime));

        Page<DynamicEntity> page = (Page)dynamicMapper.selectList(wrapper);

        PageResult<DynamicVo> pageResult = BeanCopyUtils.copyPageResult(page, DynamicVo.class);

        return pageResult;
    }

}
